package com.woniuxy.ptapp.app.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.ptapp.app.util.CheckOrderNumPayState;
import com.woniuxy.ptapp.infra.model.ClassesOrder;
import com.woniuxy.ptapp.service.ClassesOrderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
@RocketMQMessageListener(consumerGroup = "Check",topic = "CheckPayState")
public class CheckPayState implements RocketMQListener {
    @Resource
    ClassesOrderService classesOrderService;
    @Resource
    StringRedisTemplate srt;

    @Override
    public void onMessage(Object o) {
        System.out.println(o);
        //消费者幂等性
        String s = srt.opsForValue().get("yoga-pt:Check"+o);
        if(s!=null){
            if(s.equals("exist")){
                System.out.println("检查重复消费,结束方法==========");
                return;
            }
        }
        srt.opsForValue().set("yoga-pt:Check"+o,"exist");
        System.out.println("进入消费者消费orderState");
        boolean check = CheckOrderNumPayState.check(o.toString());
        ClassesOrder classesOrder = classesOrderService.getOne(new QueryWrapper<ClassesOrder>().eq("order_num", o));
        System.out.println(check);
        if(check&&classesOrder.getState()==4){
            classesOrder.setState(1);
            //修改支付方式
            classesOrder.setPayment(0);
            boolean b = classesOrderService.saveOrUpdate(classesOrder);
        }else {
            //订单交易不存在说明用户超时未支付
            ClassesOrder classesOrder1 = classesOrderService.getOne(new QueryWrapper<ClassesOrder>().eq("order_num", o));
            classesOrder1.setState(11);
            boolean save = classesOrderService.saveOrUpdate(classesOrder1);
        }
    }
}
